package com.chen.blue.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.chen.blue.converter.system.UserRoleConverter;
import com.chen.blue.domain.delete.system.UserRoleDelete;
import com.chen.blue.domain.dto.system.UserRoleDto;
import com.chen.blue.domain.dto.system.update.UserRoleDtoEdit;
import com.chen.blue.domain.model.system.RoleModel;
import com.chen.blue.domain.model.system.UserRoleModel;
import com.chen.blue.exception.e.DtoNullException;
import com.chen.blue.mapper.system.UserRoleMapper;
import com.chen.blue.result.DataResult;
import com.chen.blue.service.candidate.CandidateInfoService;
import com.chen.blue.service.system.IRoleService;
import com.chen.blue.service.system.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户和角色关联表 服务实现类
 * </p>
 *
 * @author CLT
 * @since 2024-04-28
 */
@Service("iUserRoleService")
public class IUserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleModel> implements IUserRoleService {

    @Resource
    private IRoleService iRoleService;

    @Override
    public boolean addUserRole(List<UserRoleDto> userRoleDtoList) throws DtoNullException {

        if (userRoleDtoList.isEmpty()) {
            throw new DtoNullException("参数为空");
        }

        CandidateInfoService.setCreator(userRoleDtoList);

        return super.saveBatch(UserRoleConverter.dtoListToModelList(userRoleDtoList));

    }

    @Override
    public boolean deleteUserRole(UserRoleDelete userRoleDelete) throws DtoNullException {

        if (Objects.isNull(userRoleDelete.getUserId()) && Objects.isNull(userRoleDelete.getUserIds())) {
            throw new DtoNullException("参数为空");
        }

        Map<String, Object> wrap = new HashMap<>();
        wrap.put("user_id", userRoleDelete.getUserId());

        return Objects.nonNull(userRoleDelete.getUserIds()) ? super.removeByIds(userRoleDelete.getUserIds()) : super.removeByMap(wrap);

    }

    @Override
    public boolean updateUserRole(List<UserRoleDtoEdit> userRoleDtoEditList) throws DtoNullException {

        if (Objects.isNull(userRoleDtoEditList) || userRoleDtoEditList.isEmpty())
            return false;


        CandidateInfoService.setUpdater(userRoleDtoEditList);

        // 将指定的用户的角色全部删除
        UserRoleDtoEdit userRoleDtoEdit = userRoleDtoEditList.get(0);
        Long userId = userRoleDtoEdit.getUserId();
        Map<String, Object> wrap = new HashMap<>();
        wrap.put("user_id", userId);
        super.removeByMap(wrap);

        // 重新添加角色
        return super.saveBatch(UserRoleConverter.editDtoListToModelList(userRoleDtoEditList));

    }

    @Override
    public DataResult<List<String>> queryRoleById(Long userId) throws DtoNullException {

        if (Objects.isNull(userId)) {
            throw new DtoNullException("参数为空");
        }

        LambdaQueryWrapper<UserRoleModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRoleModel::getUserId, userId);
        List<Long> roleIds = super.list(wrapper).stream().map(UserRoleModel::getRoleId).collect(Collectors.toList());


        List<String> roleNames = null;
        if (!roleIds.isEmpty()) {
            roleNames = iRoleService.listByIds(roleIds).stream().map(RoleModel::getName).collect(Collectors.toList());
        }

        DataResult<List<String>> dataResult = new DataResult<>();
        dataResult.setData(roleNames);

        return dataResult;

    }

    @Override
    public DataResult<List<String>> queryRoleCodeById(Long userId) throws DtoNullException {

        if (Objects.isNull(userId)) {
            throw new DtoNullException("参数为空");
        }

        LambdaQueryWrapper<UserRoleModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRoleModel::getUserId, userId);
        List<Long> roleIds = super.list(wrapper).stream().map(UserRoleModel::getRoleId).collect(Collectors.toList());


        List<String> roleCodes = null;
        if (!roleIds.isEmpty()) {
            roleCodes = iRoleService.listByIds(roleIds).stream().map(RoleModel::getCode).collect(Collectors.toList());
        }

        DataResult<List<String>> dataResult = new DataResult<>();
        dataResult.setData(roleCodes);

        return dataResult;

    }

    @Override
    public DataResult<List<Long>> queryRoleIdById(Long userId) throws DtoNullException {

        if (Objects.isNull(userId)) {
            throw new DtoNullException("参数为空");
        }

        LambdaQueryWrapper<UserRoleModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRoleModel::getUserId, userId);
        List<Long> roleIds = super.list(wrapper).stream().map(UserRoleModel::getRoleId).collect(Collectors.toList());

        DataResult<List<Long>> dataResult = new DataResult<>();
        dataResult.setData(roleIds);

        return dataResult;

    }

}
